﻿using DNFImagePacks2Manager.Helpers;
using System.Collections.Generic;
using System.IO;

namespace DNFImagePacks2Manager.NPK
{
    public class NPKHeader
    {
        public static readonly byte[] ValidatedFlagBytes = [
            0x4E, 0x65, 0x6F, 0x70, 0x6C, 0x65, 0x50, 0x61, 0x63, 0x6B, 0x5F, 0x42, 0x69, 0x6C, 0x6C, 0x00
            ];

        private readonly byte[] flagBytes;

        public string Flag { get; }
        public int Count { get; set; }

        public NPKHeader(BinaryReader reader)
        {
            flagBytes = reader.ReadBytes(16);
            byte[] countBytes = reader.ReadBytes(4);

            Flag = ByteHelper.Bytes2String(flagBytes);
            Count = ByteHelper.Bytes2Int(countBytes);
        }

        public NPKHeader(int imgCount)
        {
            Count = imgCount;

            flagBytes = ValidatedFlagBytes;
            Flag = ByteHelper.Bytes2String(flagBytes);
        }

        public bool ValidateFlag()
        {
            return ByteHelper.BytesEuqals(flagBytes, ValidatedFlagBytes);
        }

        public byte[] GetBytes()
        {
            List<byte> byteList = [];
            byteList.AddRange(flagBytes);
            byteList.AddRange(ByteHelper.Int2Bytes(Count));
            return [.. byteList];
        }
    }
}
